<script lang="jsx">
import { useRoute } from 'vue-router';
import { nextTick, ref } from 'vue';
import ProductionReportPage from '@/components/common/production-report-page/index.vue';
export default {
  setup() {
    const route = useRoute();

    const fileName = getfileName();
    function getfileName() {
      const fullPathArr = route.fullPath.split('/');
      return fullPathArr[fullPathArr.length - 1];
    }

    function getConfig() {
      const pages = import.meta.glob('./page/*/*.jsx', { eager: true });
      let pageConfig;
      Object.entries(pages).forEach(([path, config]) => {
        const pathArr = path.split('/');
        const fullPathFileName = pathArr[pathArr.length - 1];
        if (fullPathFileName === `${fileName}.jsx`) {
          pageConfig = config;
        }
      });
      return pageConfig.default;
    }
    const pageConfig = getConfig();
    let isPostId = false;
    if (!pageConfig.formId) {
      pageConfig.formId = fileName;
      isPostId = true;
    }
    const pageRef = ref(null);
    nextTick().then(() => {
      if (pageConfig.getPageRef) {
        pageConfig.getPageRef(pageRef);
      }
      if (pageConfig.pageMounted) {
        pageConfig.pageMounted();
      }
    });
    console.log('pageConfig :>> ', pageConfig);
    return () => (
      <ProductionReportPage
        routeName={isPostId ? `production-management:${fileName}` : fileName}
        {...pageConfig}
        ref={pageConfig.pageRef || pageRef}
        onModalClose={() => pageConfig.modalClose && pageConfig.modalClose()}
        onOpenModal={val => pageConfig.openModal && pageConfig.openModal(val)}
        onAddSuccess={val => {
          pageConfig.addSuccess && pageConfig.addSuccess(val);
        }}
      >
        {pageConfig?.rednderComponents?.map(item => {
          return item();
        })}
      </ProductionReportPage>
    );
  }
};
</script>
